package com.sumer.psychology.common.domain.model;

import com.sumer.psychology.common.Assert;
import com.sumer.psychology.common.BusinessException;
import com.sumer.psychology.common.port.adapter.service.UserInRoleAdapter;

/**
 * 获得人员的服务，隐藏请求细节
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/11/20
 * Time: 9:54
 */
public class UserService {
    private UserInRoleAdapter userInRoleAdapter;

    public UserService(UserInRoleAdapter userInRoleAdapter) {
        this.userInRoleAdapter = userInRoleAdapter;
    }

    /**
     * 获得一个作者
     *
     * @param author an identity
     * @return author
     */
    public Author authorFrom(Author author) {
        Assert.notEmpty(author.identity());

        User anAuthor = userInRoleAdapter.toUser(author.identity());
        return Author.fromUser(anAuthor);
    }

    /**
     * 获得一个教师
     *
     * @param anIdentity an identity
     * @return author
     */
    public Teacher teacherFrom(String anIdentity) {
        User aTeacher = userFrom(anIdentity);

        if (aTeacher.role() == Role.TEACHER) {
            return (Teacher) aTeacher;
        } else {
            throw new BusinessException(
                    "id为：" + anIdentity + "的人员不是教师");
        }
    }

    /**
     * 获得一个普通用户
     *
     * @param anIdentity an identity
     * @return user
     */
    public User userFrom(String anIdentity) {
        return userInRoleAdapter.toUser(anIdentity);
    }

    /**
     * 获得用户ID
     *
     * @param uId   uid
     * @param uIdPk uid pk
     * @return id
     */
    public String userId(String uId, String uIdPk) {
        return userInRoleAdapter.userId(uId, uIdPk);
    }

    /**
     * 编辑User信息
     *
     * @param aChangeUserProfileCommand change user profile
     */
    public void editUser(ChangeUserProfileCommand aChangeUserProfileCommand) {
        userInRoleAdapter.editUser(
                aChangeUserProfileCommand.identity(),
                aChangeUserProfileCommand.toParameters());
    }

    /**
     * 修改密码
     *
     * @param aChangePasswordCommand change password
     */
    public void changePassword(ChangePasswordCommand aChangePasswordCommand) {
        userInRoleAdapter.changePassword(
                aChangePasswordCommand.userId(),
                aChangePasswordCommand.toParameters());
    }

}
